<html xmlns="http://www.w3.org/1999/xhtml"><head><style type="text/css">
dt {
  font-style: italic;
  margin-top: 15px;
  margin-bottom: 3px;
  margin-left: 0px;
  border-bottom: 1px dotted black;
}
dd {
  margin-left: 10px;
}
table {
  border-collapse:collapse;
  border: 1px solid black;
  margin-top: 7px;
}
th {
  border: 1px solid black;
  padding: 3 7 3 7;
}
td {
  border: 1px solid black;
  padding: 3 7 3 7;
}
</style></head><body><p><a name="checkcast"></a><b>checkcast</b> :
  ensure type of an object or array : <a href="opcodes.html">index</a> : <a href="http://asm.objectweb.org/asm40/javadoc/user/org/objectweb/asm/MethodVisitor.html#visitTypeInsn(int,%20java.lang.String)">visitTypeInsn()</a></p><dl><dt>Description</dt><dd>checkcast checks that the top item on the operand stack (a
reference to an object or array) can be cast to a given type. For example, if
you write in Java:<br></br><pre>
return ((String)obj);
</pre>
then
the Java compiler will generate something like:<br></br><pre>
aload_1                        ; push -obj- onto the stack
checkcast java/lang/String     ; check its a String
areturn                        ; return it
</pre>
checkcast
is actually a shortand for writing Java code like:<br></br><pre>
if (! (obj == null  ||  obj instanceof &lt;class&gt;)) {
    throw new ClassCastException();
}
// if this point is reached, then object is either null, or an instance of
// &lt;class&gt; or one of its superclasses.
</pre>
In
Jasmin, checkcast takes a single parameter, &lt;type&gt;.
&lt;type&gt; is either the name of a Java class or interface, or it is
the type descriptor of an array. At runtime, the symbolic name given by
&lt;type&gt; is resolved to  a Java class (see Chapter 7 for a description of
how classes are resolved). Next, checkcast examines the top item on
the stack. If objectref belongs to &lt;type&gt; (or one of its
subclasses), or if it is null, the objectref is left on the stack and
execution continues at the subsequent instruction. If not, a
ClassCastException is thrown.</dd><dt>Exceptions</dt><dd>ClassCastException - the object on the stack is not an instance of the
specified class</dd><dt>Example</dt><dd><pre>
; push object in local variable 1 onto stack
aload_1
; check if it is an instance of Throwable or one of its subclasses.
checkcast java/lang/Throwable
; if execution reaches here, the object in local variable 1
; is still on the stack, and is either null or a Throwable object.
</pre><br></br>
<pre>
; ---
; Note that checkcast can also be used to check that an array belongs to a given type, 
; e.g. to check that local variable 1 contains an array of Strings, use:
aload_1
checkcast [Ljava/lang/String;
; if execution reaches here, the  object on the stack is an array of Strings, or it is null.
</pre></dd><dt>See also</dt><dd><a href="ref-instanceof.html">instanceof</a> </dd><dt>Stack</dt><dd><table xmlns="">
<tr>
<td>
<b>Before </b>
</td>
<td><b>After</b></td></tr>
<tr>
<td>objectref</td>
<td>objectref</td></tr>
<tr>
<td>...</td>
<td>...</td></tr>
</table></dd><dt>Bytecode</dt><dd><table xmlns="">
<tr>
<td>
<b> Type </b>
</td>
<td><b>Description</b></td></tr>
<tr>
<td>
u1 
</td>
<td>checkcast
opcode = 0xC0 (192)
</td></tr>
<tr>
<td>
u2 
</td>
<td>index</td></tr>
</table></dd></dl></body></html>